﻿namespace PillBox.Client.ViewModels
{
    using System.Collections.Generic;
    using System.ComponentModel.Composition;
    using PillBox.Client.Framework;
    using PillBox.Client.Model;

    [Export]
    public class ScheduleSelectionViewModel : ViewModelBase
    {
        private static ScheduleSelectionChoiceViewModel ALL_SELECTION =
            new ScheduleSelectionChoiceViewModel { PersonID = -1, DisplayText = "All" };

        public IEnumerable<ScheduleSelectionChoiceViewModel> Schedules
        {
            get;
            set;
        }

        public override void Activate()
        {
            ResultEnumerator.Begin(this.LoadPeople());
        }

        private IEnumerable<IResult> LoadPeople()
        {
            var peopleQuery = new GetPeopleQuery { Filter = x => x.Role == PersonRole.Patient };

            var query = peopleQuery.AsResult();

            yield return query;

            List<ScheduleSelectionChoiceViewModel> schedules = new List<ScheduleSelectionChoiceViewModel>();
            schedules.Add(ALL_SELECTION);

            foreach (var person in query.Response)
            {
                schedules.Add(
                    new ScheduleSelectionChoiceViewModel
                    {
                        PersonID = person.PersonID,
                        DisplayText = person.Name,
                        DisplayImage = person.FullSizeImageUri,
						DisplayColor = person.ColorBand
                    });
            }

            this.Schedules = schedules;

            RaisePropertyChanged(() => Schedules);
        }
    }
}